<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muser extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    
    public function loginUser($uname, $upass)
    {
        $pass_encode = md5(md5($uname).md5($upass));
        
        $sql = $this->db->query("SELECT id,username,fullname FROM user WHERE username = '".$uname."' AND password = '".$pass_encode."' ");
        if($sql->num_rows() > 0)
        {
            $data           = $sql->row();
            $data = array(
                'user_id'        => $data->id,
                'username'       => $data->username,
                'fullname'       => $data->fullname
            );
            $this->session->set_userdata($data);
            return '200';
        }
        return '201';
    }
    
    
    public function checkReConnect($id_user)
    {
        $check = $this->db->query('SELECT id FROM score WHERE user_id = '.$id_user.' AND stop = 1')->num_rows();
        if($check == 1)
        {
            return (object)array('code' => 500);
        }else{
            $number_question = $this->session->userdata('number_question') ? $this->session->userdata('number_question') : 0;
            $number_gameShow = $this->session->userdata('number_gameShow') ? $this->session->userdata('number_gameShow') : 0;
            $_data = $this->db->query('SELECT numberQuestion FROM number_play_current WHERE numberQuestion = '.$number_question.' AND numberGameshow = '.$number_gameShow);
            @$numberQuestion = $_data->row_object()->numberQuestion;
            $_check = $_data->num_rows();
            if($_check == 1)
            {
                
                $sql = "SELECT id,stt,content FROM questions WHERE id_campaign = '".$number_gameShow."' ORDER BY stt ASC LIMIT ".($numberQuestion - 1).", 1";
                $query = $this->db->query($sql);
                $data = $query->result_array();
                //echo '<pre>'; 
                //die(print_r($data));
                
                $count = sizeof($data);
                $arr = array();
                $i = 'data_';
                foreach($data AS $key=>$value)
                {
                    $arr['data_'] = json_decode($value['content']);
                    $arr['data_']->id = $value['id'];
                    $arr['data_']->stt = $value['stt'];
                }
                return (object)array('code' => 200, 'data' => (object)$arr);
            }else{
                return (object)array('code' => 201);
            }
        }
    }
    
    public function saveplaycurrentMd()
    {
        $f_1 = $this->getParamInt('cfv');
        $f_2 = $this->getParamInt('cfv_2');
        
        if($this->getParamInt('stc') == 2)
        {
            $data = array('checkstart' => 'start', 'number_question' => $f_2, 'number_gameShow' => $f_1);
        }else{
            
            $data = array('checkstart' => 'start', 'number_question' => $f_2);
        }
        $this->session->set_userdata($data);
        die($f_2.'__');
    }
    
    public function registerUser($username,$password)
    {
        $pass_encode = md5(md5($username).md5($password));
        
        $data = array(
            'username' => $username,
            'password' => $pass_encode,
            'created'  => date('Y-m-d H:i:s')
        );
        
        $this->db->insert('user', $data);
        return 200;
    }
    
    public function getDataUser($id)
    {
        return $this->db->query('SELECT * FROM user WHERE id = '.$id)->row_object();
    }
    
    public function saveInfo($id)
    {
        $data = array(
                'fullname'  => $this->getParamString('fullname'),
                'phone'     => $this->getParamString('phone'),
                'address'   => $this->getParamString('address'), 
                'email'     => $this->getParamString('email')
            );
        $this->db->update('user', $data, 'id ='.$id);
        return 200;
    }
    
    public function changePassWd($id)
    {
        $info = $this->db->query('SELECT * FROM user WHERE id = '.$id)->row_object();
        
        $password = $this->getParamString('passnew');
        $pass_encode = md5(md5($info->username).md5($password));
        
        $this->db->update('user', array('password' => $pass_encode), 'id = '.$id);
        return 200;
    }
    
    public function getTrade($number = 0,$offset = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT lt.*,u.username, DATE_FORMAT(lt.created, '%H:%i:%s %d/%m/%Y') AS time FROM logTrade AS lt LEFT JOIN user AS u ON lt.id_user = u.id ORDER BY lt.id DESC";
        
        $count = $this->db->query($sql)->num_rows();
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function regDB($user, $passwd_new, $email)
    {
        
        $tr = $this->db->query('SELECT fbreg FROM user WHERE LOWER(username) = "'.$user.'" OR LOWER(email) = "'.strtolower($email).'"');
        $numRow = $tr->num_rows();
        if($numRow > 0)
        {
            $fb = $tr->row_object()->fbreg;
            if($fb == 1)
            {
                return (object)array('status' => '200', 'password' => $passwd_new);
            }else{
                return (object)array('status' => '201');   
            }
        }
        
        
        $data = array(
                'username'      => $user,
                'email'         => $email,
                'fbreg'         => 1,
                'password'      => md5(md5($user).md5($passwd_new)),
                'created'       => date('Y-m-d H:i:s'),
                );
        $this->db->insert('user', $data);

        return (object)array('status' => '200', 'password' => $passwd_new);
    }
    
}
